package com.morehair.service.admin.impl;

import com.morehair.bean.pojo.GoodsStat;
import com.morehair.bean.pojo.OrderStat;
import com.morehair.bean.pojo.UserStat;
import com.morehair.bean.vo.admin.GoodsStatVo;
import com.morehair.bean.vo.admin.OrderStatVo;
import com.morehair.bean.vo.admin.UserStarVo;
import com.morehair.mapper.GoodsMapper;
import com.morehair.mapper.OrderMapper;
import com.morehair.mapper.UserMapper;
import com.morehair.service.admin.StatisticstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/6 21:25
 * @Description:
 **/

@Service
@Transactional
public class StatisticsServiceImpl implements StatisticstService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserStarVo statUser() {
        List<UserStat> userStats=userMapper.selectUserStat();
        ArrayList<UserStarVo.RowsBean> rowsBeans = new ArrayList<>();
        for (UserStat userStat : userStats) {
            UserStarVo.RowsBean rowsBean = new UserStarVo.RowsBean();
            rowsBean.setUsers(userStat.getUsers());
            rowsBeans.add(rowsBean);
        }
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");

        UserStarVo userStarVo = new UserStarVo();
        userStarVo.setColumns(columns);
        userStarVo.setRows(rowsBeans);
        return userStarVo;
    }

    @Autowired
    OrderMapper orderMapper;

    @Override
    public OrderStatVo statOrder() {
        List<OrderStat> orderStats=orderMapper.selectOrderStat();
        ArrayList<OrderStatVo.RowsBean> rowsBeans = new ArrayList<>();
        for (OrderStat orderStat : orderStats) {
            OrderStatVo.RowsBean rowsBean = new OrderStatVo.RowsBean();
            rowsBean.setOrders(orderStat.getOrders());
            rowsBean.setCustomers(orderStat.getCustomers());
            rowsBean.setAmount(orderStat.getAmount());
            rowsBean.setDay(orderStat.getDay());
            rowsBean.setPcr(orderStat.getAmount()/orderStat.getCustomers());
            rowsBeans.add(rowsBean);
        }
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");

        OrderStatVo orderStatVo = new OrderStatVo();
        orderStatVo.setColumns(columns);
        orderStatVo.setRows(rowsBeans);
        return orderStatVo;
    }

    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public GoodsStatVo statGoods() {
        List<GoodsStat> goodsStats=goodsMapper.selectGoodsStat();
        ArrayList<GoodsStatVo.RowsBean> rowsBeans = new ArrayList<>();
        for (GoodsStat goodsStat : goodsStats) {
            GoodsStatVo.RowsBean rowsBean = new GoodsStatVo.RowsBean();
            rowsBean.setDay(goodsStat.getDay());
            rowsBean.setAmount(goodsStat.getAmount());
            rowsBean.setOrders(goodsStat.getOrders());
            rowsBean.setProducts(goodsStat.getProducts());
            rowsBeans.add(rowsBean);
        }
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");

        GoodsStatVo goodsStatVo = new GoodsStatVo();
        goodsStatVo.setColumns(columns);
        goodsStatVo.setRows(rowsBeans);
        return goodsStatVo;
    }
}
